package com.example.yichuangyun.mvvm.ui.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.yichuangyun.R;
import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.databinding.ActivityRiskInfoBinding;
import com.example.yichuangyun.model.company.CompanyIntroModel;
import com.example.yichuangyun.model.company.CompanyLabelModel;
import com.example.yichuangyun.model.company.GsInfoAllModel;
import com.example.yichuangyun.mvvm.base.BaseActivity;
import com.example.yichuangyun.mvvm.ui.activity.view.GsInfoViewModel;
import com.example.yichuangyun.mvvm.ui.adapter.GsBaseInfoAdapter;
import com.example.yichuangyun.mvvm.ui.adapter.InfoTabAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 工商信息
 */
@SuppressLint("NotifyDataSetChanged")
public class GsInfoActivity extends BaseActivity<ActivityRiskInfoBinding, GsInfoViewModel> {

    private String eid;
    private final List<CompanyLabelModel> tabData = new ArrayList<>();
    private InfoTabAdapter tabAdapter;
    private final List<GsInfoAllModel> data = new ArrayList<>();
    private int mPos;
    private LinearLayoutManager manager;

    @Override
    protected GsInfoViewModel bindModel() {
        return getViewModel(this, GsInfoViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initData() {
        initTitleBar(getString(R.string.gsxx));
        tabData.add(new CompanyLabelModel(getString(R.string.yyzzxx)));
        tabData.add(new CompanyLabelModel(getString(R.string.gdxx)));
        tabData.add(new CompanyLabelModel(getString(R.string.zyry)));
        tabData.add(new CompanyLabelModel(getString(R.string.gsbg)));
        tabData.add(new CompanyLabelModel(getString(R.string.qynb)));
        tabAdapter = new InfoTabAdapter(context, tabData);

        data.add(new GsInfoAllModel(getString(R.string.yyzzxx)));
        data.add(new GsInfoAllModel(getString(R.string.gdxx)));
        data.add(new GsInfoAllModel(getString(R.string.zyry)));
        data.add(new GsInfoAllModel(getString(R.string.gsbg)));
        data.add(new GsInfoAllModel(getString(R.string.qynb)));

        manager = new LinearLayoutManager(this);
        binding.jyycRecyclerView.setLayoutManager(manager);
        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            eid = bundle.getString(Constant.eidKey);
            int position = bundle.getInt(Constant.positionKey);
            tabData.get(position).setSelect(true);
            data.get(position).setOpen(true);
            LinearLayoutManager linearLayoutManager = (LinearLayoutManager) binding.recyclerView.getLayoutManager();
            if (null != linearLayoutManager) {
                linearLayoutManager.scrollToPositionWithOffset(position, 0);
            }
            viewModel.getGsBaseInfo(eid);
        }
    }

    @Override
    protected void initClick() {
        // 基本信息返回结果
        viewModel.gsBaseInfoModelMutableLiveData.observe(this, gsBaseInfoModel -> {
            List<CompanyIntroModel> introData = new ArrayList<>();
            CompanyIntroModel companyIntroModel = new CompanyIntroModel(gsBaseInfoModel.getName(),
                    gsBaseInfoModel.getCreditNo(), gsBaseInfoModel.getOperName(), gsBaseInfoModel.getRegistCapi(),
                    gsBaseInfoModel.getStartDate(), gsBaseInfoModel.getNewStatusCode(),
                    gsBaseInfoModel.getIndustryCodeList(), gsBaseInfoModel.getAddress(),
                    gsBaseInfoModel.getReportAddress(), gsBaseInfoModel.getRegNo(),
                    gsBaseInfoModel.getOrgNo(), gsBaseInfoModel.getBelongOrg(), gsBaseInfoModel.getCheckDate(),
                    gsBaseInfoModel.getActualCapi(), gsBaseInfoModel.getEconKind(), gsBaseInfoModel.getTermStart(),
                    gsBaseInfoModel.getTermEnd(), gsBaseInfoModel.getScope(), gsBaseInfoModel.getReportYear());
            introData.add(companyIntroModel);
            data.get(0).setInfoList(introData);
            // 股东信息
            if (null != gsBaseInfoModel.getPartnersAlls() && gsBaseInfoModel.getPartnersAlls().size() > 0) {
                data.get(1).setPartnersAlls(gsBaseInfoModel.getPartnersAlls());
                tabData.get(1).setCount(gsBaseInfoModel.getPartnersAlls().size());
            }
            // 主要人员
            if (null != gsBaseInfoModel.getEmployeesAlls() && gsBaseInfoModel.getEmployeesAlls().size() > 0) {
                data.get(2).setEmployeesAlls(gsBaseInfoModel.getEmployeesAlls());
                tabData.get(2).setCount(gsBaseInfoModel.getEmployeesAlls().size());
            }
            // 企业年报
            if (null != gsBaseInfoModel.getReportDetails() && gsBaseInfoModel.getReportDetails().size() > 0) {
                data.get(4).setReportDetails(gsBaseInfoModel.getReportDetails());
                tabData.get(4).setCount(gsBaseInfoModel.getReportDetails().size());
            }
            viewModel.getGsChangeRecord(eid);
        });

        // 基本信息返回结果错误
        viewModel.errorBaseInfoMutableLiveData.observe(this, s -> {
            viewModel.getGsChangeRecord(eid);
        });

        // 工商变更返回结果
        viewModel.gsChangeRecordDataModelMutableLiveData.observe(this, gsChangeRecordDataModel -> {
            if (null != gsChangeRecordDataModel.getRecords() && gsChangeRecordDataModel.getRecords().size() > 0) {
                data.get(3).setRecords(gsChangeRecordDataModel.getRecords());
                data.get(3).setCount(gsChangeRecordDataModel.getTotal());
                tabData.get(3).setCount(gsChangeRecordDataModel.getTotal());
            }
            setTabAdapter();
        });

        // 工商变更返回结果错误
        viewModel.errorGsChangeMutableLiveData.observe(this, s -> setTabAdapter());

        binding.jyycRecyclerView.setOnScrollChangeListener((view, i, i1, i2, i3) -> {
            int position = manager.findFirstVisibleItemPosition();
            if (position != mPos) {
                mPos = position;
                LinearLayoutManager linearLayoutManager = (LinearLayoutManager) binding.recyclerView.getLayoutManager();
                if (null != linearLayoutManager) {
                    linearLayoutManager.scrollToPositionWithOffset(position, 0);
                    for (CompanyLabelModel labelModel : tabData) {
                        labelModel.setSelect(false);
                    }
                    tabData.get(position).setSelect(true);
                    tabAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    private void setTabAdapter() {
        tabAdapter = new InfoTabAdapter(context, tabData);
        binding.recyclerView.setAdapter(tabAdapter);
        tabAdapter.setOnTabItemClickListener(position -> {
            for (CompanyLabelModel labelModel : tabData) {
                labelModel.setSelect(false);
            }
            tabData.get(position).setSelect(true);
            tabAdapter.notifyDataSetChanged();
            LinearLayoutManager linearLayoutManager = (LinearLayoutManager) binding.jyycRecyclerView.getLayoutManager();
            if (null != linearLayoutManager) {
                linearLayoutManager.scrollToPositionWithOffset(position, 0);
            }
        });
        GsBaseInfoAdapter adapter = new GsBaseInfoAdapter(context, data);
        binding.jyycRecyclerView.setAdapter(adapter);
    }
}